var $ = getApp(), methods = require("../methods.js");
Page(Object.assign({}, methods, {
	data: {
		pagenum: 1,
		clist: [],
		curIndex: 0,
		prolist: [],
		curCid: 0,
		isload: 0
	},
	_onLoad: function (options) {
		var that = this;
		
		var curCid = 0;
		if (that.options && that.options.cid) {
			curCid = that.options.cid
		}
		if (that.options && that.options.scene) {
			var scene = that.options.scene
			var scenes = scene.split('_');
			if(scenes[1]){
				curCid = parseInt(scenes[1]);
			}
		}
		that.setData({curCid:curCid})
		that.setData({ pagenum: 1, prolist: [], $nomoreData: false });
		$.get('business/clist', {}, function (res){
			that.setData(res)
			wx.getLocation({
				type: 'gcj02',
				success:function(res) {
					var latitude = res.latitude
					var longitude = res.longitude
					that.setData({longitude:longitude,latitude:latitude})
					that.getblist();
				},
				fail:function(){
					that.getblist();
				}
			});
		});
	},
	onPullDownRefresh: function () {
		//this.onLoad();
		wx.stopPullDownRefresh()
	},
	scrolltolower:function(){
		if (!this.data.$dataEmpty.visible && !this.data.$nomoreData) {
			this.setData({ pagenum: this.data.pagenum + 1 })
			this.getblist();
		}
	},
	getblist: function () {
		var that = this
		var pagenum = that.data.pagenum
		var cid = that.data.curCid;
		var longitude = that.data.longitude
		var latitude = that.data.latitude
		$.post('business/clist', { longitude:longitude,latitude:latitude,pagenum: pagenum,cid: cid}, function (res) {
			var data = res.data
			if (pagenum == 1) {
				that.setData({ blist: data });
				if (data.length == 0) {
					$.showEmpty('没有相关商家','/images/error2.png');
				}
			} else {
				if (data.length == 0) {
					//$.error('没有更多数据了');
					that.setData({ $nomoreData: true });
				} else {
					var blist = that.data.blist
					var newdata = blist.concat(data)
					that.setData({ blist: newdata });
				}
			}
		});
	},
	//事件处理函数
	switchRightTab: function (e) {
		var that = this
		var id = e.target.dataset.id;
		var index = parseInt(e.target.dataset.index);
		that.setData({
			curIndex: index,
			blist: [],
			curCid: id
		});
		that.setData({
			pagenum: 1,
			blist: [],
			$nomoreData: false
		});
		this.getblist();
	},
}))